<template>
  <el-card class="main-card">
    <div class="title">{{ this.$route.name }}</div>
    <div class="operation-container">
      <el-button
        type="primary"
        size="small"
        icon="el-icon-plus"
        @click="openModel(null)"
      >
        新增
      </el-button>

      <div style="margin-left: auto">
        <el-input
          v-model="searchCondition.keywords"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="请输入标签名"
          style="width: 200px"
          @keyup.enter.native="searchTags"
        />
        <el-button
          type="primary"
          size="small"
          icon="el-icon-search"
          style="margin-left: 1rem"
          @click="searchTags"
        >
          搜索
        </el-button>
      </div>
    </div>

    <!-- 表格展示 -->
    <el-table :data="tagList" style="width: 100%" v-loading="loading" border>
      <el-table-column prop="name" label="标签名" width="350" align="center">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.name }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="articleCount"
        label="文章数"
        width="380"
        align="center"
      >
      </el-table-column>

      <el-table-column prop="createTime" label="创建时间" align="center ">
        <template slot-scope="scope">
          <i class="el-icon-time" style="margin-right: 5px" />
          {{ scope.row.createTime | date }}
        </template>
      </el-table-column>

      <!-- 列操作 -->
      <el-table-column label="操作" align="center" width="160">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="openModel(scope.row)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            slot="reference"
            @click="deleteTag(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        class="pagination-container"
        background
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="searchCondition.current"
        :page-size="searchCondition.size"
        :total="count"
        :page-sizes="[5, 10, 15, 20]"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </div>
    <!-- 编辑对话框 -->
    <el-dialog :visible.sync="addOrEdit" width="30%">
      <div class="dialog-title-container" slot="title" ref="tagTitle" />
      <el-form label-width="80px" size="medium" :model="tagForm">
        <el-form-item label="标签名">
          <el-input style="width: 220px" v-model="tagForm.name" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addOrEdit = false">取 消</el-button>
        <el-button type="primary" @click="addOrEditTag"> 确 定 </el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import tagApi from "@/api/tag";
export default {
  data() {
    return {
      // isDelete: false,
      loading: false,
      addOrEdit: false,
      // 搜索关键词
      keywords: null,
      // 标签封装
      tagForm: {
        id: null,
        name: "",
      },
      // 标签列表
      tagList: [],
      // 分页相关
      // 总数据量
      count: 0,
      searchCondition: {
        // 页码
        current: 1,
        size: 10,
        keywords: "",
      },
    };
  },
  mounted() {
    this.getTagList();
  },
  methods: {
    // 分页相关
    searchTags() {
      this.searchCondition.current = 1;
      this.getTagList();
    },
    sizeChange(size) {
      this.searchCondition.size = size;
      this.getTagList();
    },
    currentChange(current) {
      this.searchCondition.current = current;
      this.getTagList();
    },
    openModel(tag) {
      if (tag != null) {
        this.tagForm = JSON.parse(JSON.stringify(tag));
        this.$refs.tagTitle.innerHTML = "修改标签";
      } else {
        this.tagForm.id = null;
        this.tagForm.name = "";
        this.$refs.tagTitle.innerHTML = "添加标签";
      }
      this.addOrEdit = true;
    },
    // 获取标签列表
    getTagList() {
      tagApi
        .tagBackList(this.searchCondition)
        .then((result) => {
          this.count = result.data.count;
          this.tagList = result.data.recordList;
        })
        .catch((err) => {
          this.loading = true;
        });
    },
    // 添加或修改
    addOrEditTag() {
      if (this.tagForm.name.trim() == "") {
        this.$message.error("标签名不能为空");
        return false;
      }
      tagApi.addOrEditTag(this.tagForm).then((result) => {
        if (result.flag) {
          this.$notify.success({
            title: "成功",
            message: result.message,
          });
          this.getTagList();
        } else {
          this.$notify.error({
            title: "失败",
            message: result.message,
          });
        }
        this.addOrEdit = false;
      });
    },
    // 删除
    deleteTag(id) {
      this.$confirm("确定要删除该标签吗？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        tagApi.deleteTag(id).then((data) => {
          if (data.flag) {
            this.$notify.success({
              title: "成功",
              message: data.message,
            });
            this.listCategories();
          } else {
            this.$notify.error({
              title: "失败",
              message: data.message,
            });
          }
          this.isDelete = false;
          this.getTagList();
        });
      });
    },
  },
};
</script>

<style scoped>
/* .main-card {
  min-height: calc(100vh - 126px);
  padding: 20px;
  margin: 20px;
}
.operation-container {
  display: flex;
  align-items: center;
  margin-bottom: 1.25rem;
} */
</style>